package com.meme.editor.property.control;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.JSpinner;

import org.apache.log4j.Logger;

import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseWheelListener;

import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyValue;
import com.meme.editor.property.PropertyControl;

public class InputPropertyControl extends PropertyControl implements ActionListener, FocusListener//, MouseWheelListener
{
	static Logger logger = Logger.getLogger (InputPropertyControl.class);
	
	JTextField mTextField;
	String mLastValue;
	
	public InputPropertyControl ()
	{
	}
	
	@Override
	public void enableEvents ()
	{
		mTextField.addActionListener(this);
		mTextField.addFocusListener(this);
		//mTextField.addMouseWheelListener (this);
	}
	
	@Override
	public void disableEvents ()
	{
		mTextField.removeActionListener(this);
		mTextField.removeFocusListener(this);
	}
	
	@Override
	public void updateFromValue (Object[] controlParams) 
	{
		mLastValue = (String)controlParams[0];
		( (JTextField) getComponent () ).setText (mLastValue);
	}
	
	@Override
	public void updateFromValue ()
	{
		mLastValue = getPropetyValue ().toString();
		( (JTextField) getComponent () ).setText (mLastValue);
	}
	
	@Override
	public Object[] makeDataForValue ()
	{
		return new Object[] { ( (JTextField) getComponent () ).getText() };
	}
	
//	@Override
//	public void setEditable (boolean bEditable)
//	{		
//		( (JTextField) getComponent () ).setEditable (bEditable);
//	}
	
	@Override
	public JComponent getComponent ()
	{
		if ( mTextField == null )
		{
			mTextField = new JTextField ();
			mTextField.addActionListener(this);
			mTextField.addFocusListener(this);
		}
		
		mTextField.setEditable ( isEditable () );
			
		return mTextField;
	}
	
	@Override
	public void disposeComponent ()
	{
		if( mTextField!= null )
		{
			mTextField.removeActionListener(this);
			mTextField.removeFocusListener(this);
		}
		
		mTextField = null;
	}

	
	@Override
	public void actionPerformed(java.awt.event.ActionEvent e)
	{
		//sendPossibleValueModifyingMessage (e.getActionCommand());
		
		logger.assertLog ( ( (JTextField) getComponent () ).getText() .equals(e.getActionCommand()), "?" );
		
		updatePropertyValue ();
	}
	
	@Override
	public void focusGained(java.awt.event.FocusEvent arg0)
	{
		
	}
	  
	@Override
	public void focusLost(java.awt.event.FocusEvent arg0)
	{
		( (JTextField) getComponent () ).setText (mLastValue);
	}
	
	@Override
	protected boolean _setParameter (String key, Object param)
	{
		return false;
	}
}
